package com.example.template.service;

import com.example.template.dao.AccountRepository;
import com.example.template.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author BuerYouth
 */
@Service
public class AccountService {

    @Autowired
    private AccountRepository accountRepository;

    /**
     * 查询全部数据
     */
    public List<Account> loadDataAll() {
        return accountRepository.findAll();
    }

    /**
     * 搜索数据
     */
    @Cacheable(cacheNames = "loadDataAll", key = "#username")
    public List<Account> loadDataSearch(String username) {
        Account account = new Account();
        account.setUsername(username);
        Example<Account> example = Example.of(account);
        return accountRepository.findAll(example);
    }

    /**
     * 添加数据
     */
    @CacheEvict(value = "loadDataAll", allEntries = true)
    public void addData(Account account) {
        accountRepository.save(account);
    }

    /**
     * 清空accountCache 缓存
     */
    @CacheEvict(value = "loadDataAll", allEntries = true)
    public void cleanData() {}
}
